﻿using UnityEngine;
using System.Collections;

public class shipLoseSpawner : MonoBehaviour {
    public GameObject spawnpoint;
	public GameObject  myShip;
	float spawner;
	public float speed;
    private float timer;
	public GameObject explosion;
	private GameObject myshipref;
	private bool oneanddone;
	public GameObject shipPrefab;
	// Use this for initialization
	void Start () 
    {
		oneanddone = true;
        MeshFilter viewedModelFilter = (MeshFilter)myShip.GetComponent("MeshFilter");
		Vector3 enemySize = viewedModelFilter.sharedMesh.bounds.size * myShip.transform.localScale.x;
		 float minX = WithinBounds.Bounds.minX + 2*(enemySize.x);
            float maxX = WithinBounds.Bounds.maxX - 2*(enemySize.x);
		float spawnX = Random.Range(minX, maxX);
		myshipref =Instantiate(shipPrefab,new Vector3(spawnX, transform.position.y, -3),Quaternion.Euler(90, 180, transform.rotation.z)) as GameObject;
        myshipref.rigidbody.AddForce(Vector3.down * speed);	
	}
	
	// Update is called once per frame
	void Update () 
    {
	    timer += Time.deltaTime;
		if(oneanddone && timer > 3)
        {
			  Instantiate(explosion, myshipref.transform.position, transform.rotation);
			Destroy(myshipref);
			oneanddone = false;
		}
	}
}
